using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics.Proxy;


namespace OhioStateVolumeRenderer
{
    public class FisheyeManager
    {
        private static FisheyeManager instance;
        private MouseEventHandler mouseHandler;
        private System.Windows.Forms.Control target;
        private bool isShaderLinked = false;
        private ShaderProgram fisheyeShader;
        private OSUVolume osuVolume;
        private MaterialProxy currentMaterial;
        private float lensRadius;
        private float zoomLevel;

        public static FisheyeManager Instance
        {
            get
            {
                if(instance == null)
                    instance = new FisheyeManager();
                return instance;
            }
            set
            {
                if (instance == null)
                    instance = value;
            }
        }

        private FisheyeManager()
        {
        }

        public void SetModeActive(System.Windows.Forms.Control targetControl, OSUVolume osuVol)
        {
            target = targetControl;
            osuVolume = osuVol;
            if (mouseHandler == null)
            {
                mouseHandler = new MouseEventHandler(MouseMoveHandler);
            }
            target.MouseMove += mouseHandler;
            if (!isShaderLinked)
            {
                LinkTheShader();
                isShaderLinked = true;
            }
        }

        public void SetModeInactive()
        {
            if (mouseHandler == null)
            {
                return;
            }
            target.MouseMove -= mouseHandler;

            target.Invalidate();
        }

        private void MouseMoveHandler(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            double controlWidth = ((System.Windows.Forms.Control)sender).Width;
            double controlHeight = ((System.Windows.Forms.Control)sender).Height;

            double resultX = (double)e.X / controlWidth;
            double resultY = (controlHeight - e.Y)/controlHeight;

            if (currentMaterial == null)//nothing is selected in the combobox
                return;

            if (currentMaterial.Shader != fisheyeShader)
            {
                currentMaterial.Shader = fisheyeShader;
            }

            Dictionary<string, UniformVariableValues> uv = currentMaterial.UniformVariableValues;

            foreach (KeyValuePair<String, UniformVariableValues> kvp in uv)
            {
                UniformVariableValues val = kvp.Value;
                if (kvp.Key.Equals("mouseX"))
                {
                    val[0].Float = new SharedFloat((float)resultX);
                }
                else if (kvp.Key.Equals("mouseY"))
                {
                    val[0].Float = new SharedFloat((float)resultY);
                }
                else if(lensRadius == 0 && kvp.Key.Equals("lensRadius"))//if radius hasn't been set yet
                {
                    val[0].Float = new SharedFloat((float)0.14f);
                }
                else if (zoomLevel == 0 && kvp.Key.Equals("zoomLevel"))//if zoom level hasn't been set yet
                {
                    val[0].Float = new SharedFloat((float)0.14f);
                }
            }

            target.Invalidate();
        }

        private void LinkTheShader()
        {
            List<FragmentShader> fragList = new List<FragmentShader>(1);
            List<VertexShader> vertList = new List<VertexShader>(1);

            VertexShader vert = AssetManager.Instance.Find(typeof(VertexShader), "FisheyeVertShader") as VertexShader;
            FragmentShader frag = AssetManager.Instance.Find(typeof(FragmentShader), "FisheyeFragShader") as FragmentShader;

            vertList.Add(vert);
            fragList.Add(frag);

            fisheyeShader = new ShaderProgram("Fisheye Shader", vertList, fragList);
            //GLShader.Instance.Link(fisheyeShader);

            AssetManager.Instance.Add(fisheyeShader);
        }

        public void setCurrentMaterial(MaterialProxy material)
        {
            currentMaterial = material;
            if (currentMaterial.Shader != fisheyeShader)
            {
                currentMaterial.Shader = fisheyeShader;
            }
        }

        public void SetLensRadius(float radius)
        {
            this.lensRadius = radius;
            if (currentMaterial == null) return;

            Dictionary<string, UniformVariableValues> uv = currentMaterial.UniformVariableValues;
            foreach (KeyValuePair<String, UniformVariableValues> kvp in uv)
            {
                UniformVariableValues val = kvp.Value;
                if (kvp.Key.Equals("lensRadius"))
                {
                    val[0].Float = new SharedFloat((float)lensRadius);
                }
            }
        }

        public void SetZoomLevel(float zoomLevel)
        {
            this.zoomLevel = zoomLevel;
            if (currentMaterial == null) return;

            Dictionary<string, UniformVariableValues> uv = currentMaterial.UniformVariableValues;
            foreach (KeyValuePair<String, UniformVariableValues> kvp in uv)
            {
                UniformVariableValues val = kvp.Value;
                if (kvp.Key.Equals("zoomLevel"))
                {
                    val[0].Float = new SharedFloat((float)zoomLevel);
                }
            }
        }
    }
}
